package com.yiwei.hdb.http;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by MasterDong on 16/8/4.
 */
public class NetStatus {
    private static NetStatus instance;
    private Context context;
    private TelephonyManager telephonyManager;
    private ConnectivityManager connectivityManager;

    private NetStatus(Context context) {
        this.context = context;
        this.telephonyManager = (TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
        this.connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }

    public static synchronized NetStatus getInstance(Context context) {
        if(instance == null) {
            instance = new NetStatus(context);
        }

        return instance;
    }

    public static NetStatus newInstance(Context context) {
        return new NetStatus(context);
    }

    public boolean isAirModeOn(Context context) {
        return Settings.System.getInt(context.getContentResolver(), "airplane_mode_on", 0) == 1;
    }

    public void setAirplaneMode(Context context, boolean enable) {
        Settings.System.putInt(context.getContentResolver(), "airplane_mode_on", enable?1:0);
        Intent intent = new Intent("android.intent.action.AIRPLANE_MODE");
        intent.putExtra("state", enable);
        context.sendBroadcast(intent);
    }

    private NetworkInfo getNetworkInfo() {
        return this.connectivityManager.getActiveNetworkInfo();
    }

    public boolean isNetworkAvailable(){
//        //步骤1：通过Context.getSystemService(Context.CONNECTIVITY_SERVICE)获得ConnectivityManager对象
//        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//
//        //步骤2：获取ConnectivityManager对象对应的NetworkInfo对象
//        //NetworkInfo对象包含网络连接的所有信息
//        //步骤3：根据需要取出网络连接信息
//        //获取WIFI连接的信息
//        NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        Boolean isWifiConn = networkInfo.isConnected();
//
//        //获取移动数据连接的信息
//        networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//        Boolean isMobileConn = networkInfo.isConnected();
//        if (isWifiConn||isMobileConn){
//            return true;
//        }else {
//            return false;
//        }
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                android.net.Network[] networks = connectivityManager.getAllNetworks();
                NetworkInfo networkInfo;
                for (android.net.Network mNetwork : networks) {
                    networkInfo = connectivityManager.getNetworkInfo(mNetwork);
                    if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                        return true;
                    }
                }
            } else {
                //否则调用旧版本方法
                if (connectivityManager != null) {
                    NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
                    if (info != null) {
                        for (NetworkInfo anInfo : info) {
                            if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                                return true;
                            }
                        }
                    }
                }
            }
        } catch (NullPointerException e) {
            return false;
        }

        //新版本调用方法获取网络状态
        return false;
    }

    public boolean isNetowrkAvaiable(long timeout) {
        NetworkInfo activeNetworkInfo = this.getNetworkInfo();
        if(activeNetworkInfo != null && activeNetworkInfo.isAvailable() && activeNetworkInfo.isConnected()) {
            HttpURLConnection httpUrl = null;

            try {
                httpUrl = (HttpURLConnection)(new URL("http://www.baidu.com")).openConnection();
                httpUrl.setConnectTimeout((int)timeout);
                httpUrl.connect();
            } catch (IOException var9) {
                return false;
            } finally {
                if(httpUrl != null) {
                    httpUrl.disconnect();
                }

                httpUrl = null;
            }

            return true;
        } else {
            return false;
        }
    }

    public boolean isHasNetwork() {
        return this.getNetworkInfo() != null;
    }

    public int getNetworkType() {
        return this.getNetworkInfo().getType();
    }

    public String getNetworkTypeName() {
        return this.getNetworkInfo().getTypeName();
    }

    public int getSubNetworkType() {
        return this.getNetworkInfo().getSubtype();
    }

    public String getSubNetworkTypeName() {
        return this.getNetworkInfo().getSubtypeName();
    }

    public boolean isWifi() {
        return this.isHasNetwork() && this.getNetworkType() == 1;
    }

    public boolean isBluetooth() {
        return this.isHasNetwork() && this.getNetworkType() == 7;
    }


    public boolean isMobileNetwork() {
        return this.isHasNetwork() && this.getNetworkType() == 0;
    }

    public boolean is2G() {
        if(this.isHasNetwork()) {
            switch(this.getSubNetworkType()) {
                case 1:
                case 2:
                case 4:
                    return true;
                case 3:
            }
        }

        return false;
    }

    public boolean is3G() {
        if(this.isHasNetwork()) {
            switch(this.getSubNetworkType()) {
                case 3:
                case 5:
                case 6:
                case 10:
                    return true;
                case 4:
                case 7:
                case 8:
                case 9:
            }
        }

        return false;
    }

    public void setPreferedNetworkType(int mode) {
        Settings.Secure.putInt(this.context.getContentResolver(), "preferred_network_mode", mode);
        Intent intent = new Intent("com.android.phone.CHANGE_NETWORK_MODE");
        intent.putExtra("com.android.phone.NEW_NETWORK_MODE", mode);
        this.context.sendBroadcast(intent);
    }

    public int getPreferedNetworkType() throws Settings.SettingNotFoundException {
        return Settings.Secure.getInt(this.context.getContentResolver(), "preferred_network_mode");
    }

    public String getIMEI() {
        return this.telephonyManager.getDeviceId();
    }
}
